package com.ejuornal.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Store;
import com.ejuornal.service.StoreService;
import com.ejuornal.springutil.SpringUtil;

@Component("storeAction")
@Scope("prototype")
public class StoreAction extends BaseAction<Store> {
	private static final long serialVersionUID = 1L;
	private StoreService storeService;
	private Store store;
	private String articalIdString;
	private String userIdString;
	private String storeIdString;
	private String isStore;

	/**
	 * 文章收藏--ajax局部操作
	 */
	public void addStoreArtical() {
		int articalId = Integer.parseInt(articalIdString);
		int userId = super.getUserIdBySession();
		storeService = (StoreService) SpringUtil.getBean("storeService");
		Store ss = new Store();
		ss.setContentId(articalId);
		ss.setUserId(userId);
		storeService.add(ss);
		HttpServletResponse res = ServletActionContext.getResponse();
		res.reset();
		res.setContentType("text/html;charset=utf-8");
		PrintWriter pw;
		try {
			pw = res.getWriter();
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 判断文章是否已经收藏过
	 */
	public void checkIsStore() {
		int userId = super.getUserIdBySession();
		if (userId == -1) {
			isStore = "收藏";
		} else {
			int articalId = Integer.parseInt(articalIdString);
			storeService = (StoreService) SpringUtil.getBean("storeService");
			int count = storeService.checkIsStore(userId, articalId);
			if (count == 1) {
				isStore = "已收藏";
			} else {
				isStore = "收藏";
			}
		}
		HttpServletResponse res = ServletActionContext.getResponse();
		res.reset();
		res.setContentType("text/html;charset=utf-8");
		PrintWriter pw;
		try {
			pw = res.getWriter();
			pw.print(isStore);
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 文章收藏
	public String addStore() {
		int articalId = Integer.parseInt(articalIdString);
		int userId = super.getUserIdBySession();
		storeService = (StoreService) SpringUtil.getBean("storeService");
		store.setUserId(userId);
		store.setContentId(articalId);
		storeService.add(store);

		return SUCCESS;
	}

	// 文章收藏列表
	public String listStore() {
		String url = "store/listStore";
		storeService = (StoreService) SpringUtil.getBean("storeService");
		pages = storeService.findByUserId((page - 1) * 10, url, super.getUserIdBySession());

		return SUCCESS;
	}

	// 文章收藏删除
	public String deleteStore() {
		storeService = (StoreService) SpringUtil.getBean("storeService");
		int storeId = Integer.parseInt(storeIdString);
		storeService.delete(storeId);

		return SUCCESS;
	}

	public Store getStore() {
		return store;
	}

	public void setStore(Store store) {
		this.store = store;
	}

	public StoreService getStoreService() {
		return storeService;
	}

	@Resource(name = "storeService")
	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	public String getArticalIdString() {
		return articalIdString;
	}

	public void setArticalIdString(String articalIdString) {
		this.articalIdString = articalIdString;
	}

	public String getUserIdString() {
		return userIdString;
	}

	public void setUserIdString(String userIdString) {
		this.userIdString = userIdString;
	}

	public String getStoreIdString() {
		return storeIdString;
	}

	public void setStoreIdString(String storeIdString) {
		this.storeIdString = storeIdString;
	}

	public String getIsStore() {
		return isStore;
	}

	public void setIsStore(String isStore) {
		this.isStore = isStore;
	}
}